package com.topgay.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import com.topgay.domain.ChattingMessage;
import com.topgay.service.ChattingService;

@Controller
@RequestMapping("/chatting/")
public class ChattingController {
	
	@Autowired
	private ChattingService service;
	
	@RequestMapping("chattingResultIndex.do")
	public String showResultIndex(){		
		return "body/chatting/chatting.jsp";
	}
	
	@RequestMapping("loginIndex.do")
	public String showLoginIndex(){
		return "body/chatting/login_test.jsp";
	}
	
	@RequestMapping("login.do")
	public ModelAndView login(String fromId, String password){
		return new ModelAndView("body/chatting/list_test.jsp", "fromId", fromId);
	}
	
	@RequestMapping("chattingIndex.do")
	public ModelAndView showChattingIndex(String toId, String fromId){
		ModelAndView av = new ModelAndView();
		av.setViewName("body/chatting/chatting.jsp");
		av.addObject("toId", toId);
		av.addObject("fromId", fromId);
		
		return av;
	}
	
	@RequestMapping("send.do")
	public String sendMessage(String fromId, String toId, String message, ModelMap map){
		ChattingMessage tmpObj = service.sendMessage(fromId, toId ,message);	
		System.out.println(tmpObj);
		map.addAttribute("message", tmpObj);
		
		return "jsonView";
	}
	
	@RequestMapping("get.do")
	public String getMessage(String fromId, ModelMap map){
		List<ChattingMessage> m_list = service.selectMessageById(fromId);
		map.addAttribute("list", m_list);

		return "jsonView";
	}
}
